/*************************************************************************
	> File Name: log.hpp
	> Author: H.M. Lau
	> Mail: hm.lau@outlook.com
	> Created Time: 2023/09/29 23:16
 ************************************************************************/

#pragma once

#include "comm.hpp"

const int DEBUG =   0;
const int NORMAL =  1;
const int WARNING = 2;
const int ERROR =   3;
const int FATAL =   4;

const int STD_SIZE = 1024;
const int LOG_SIZE = 1024;

const char* gLevelMap[] = {
    "DEBUG",
    "NORMAL",
    "WARNING",
    "ERROR",
    "FATAL"
};

const std::string logFile = "./threadpool.log";

void logMessage(int level, const char* format, ...)
{
#ifndef DEBUG_SHOW
    if (level == DEBUG) {
        return ;
    }
#endif
    char stdBuffer[STD_SIZE];
    time_t timestamp = time(nullptr);
    snprintf(stdBuffer, sizeof stdBuffer, "[%s] [%ld] ", gLevelMap[level], timestamp);
    char logBuffer[LOG_SIZE];
    va_list args;
    va_start(args, format);
    vsnprintf(logBuffer, sizeof logBuffer, format, args);
    va_end(args);
    FILE *fp = fopen(logFile.c_str(), "a");
    fprintf(fp, "%s%s\n", stdBuffer, logBuffer);
    fclose(fp);
}
